package com.squirrel.michale.test01;

import org.w3c.dom.Node;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author guanhao 观浩
 * @version v1.0.0.0
 * @createTime 2023/3/10 18:32
 * @company Michale Squirrel
 * @link
 * @description
 */
public class Solution {


    String[] textDataTrans(String[] datas) {
        int[][] intArray = getIntArray(datas);
        int maxX = intArray.length;
        int maxY = intArray[0].length;
        String[] resultReal = new String[maxX+2];
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < maxY; i++) {
            stringBuffer.append("+").append("---");
        }
        stringBuffer.append("+");
        resultReal[0] = stringBuffer.toString();
        resultReal[maxX+1] = stringBuffer.toString();
        for (int i = 0; i < maxX; i++) {
            StringBuffer stringBuffer2 = new StringBuffer();
            stringBuffer2.append("|");
            for (int j = 0; j < maxY; j++) {
                if (intArray[i][j] == 0) {
                    stringBuffer2.append("   ");
                }else{
                    char symbol = (char) intArray[i][j];
                    stringBuffer2.append(" ").append(symbol).append(" ");
                }
                stringBuffer2.append("|");
            }
            resultReal[i+1] = stringBuffer2.toString();
        }
        return resultReal;
    }

    private int[][] getIntArray(String[] datas) {
        int count = datas.length;
        int maxX = 0;
        int maxY = 0;
        int[][] cellFullInfo = new int[count][3];
        for (int i = 0; i < datas.length; i++) {
            String info = datas[i];
            String[] cellInfo = info.split(" ");
            int length = cellInfo.length;
            int[] cell = new int[3];
            cell[0] = getLastInt(cellInfo[0]);
            maxX = Math.max(maxX, cell[0]);
            cell[1] = getLastInt(cellInfo[1]);
            maxY = Math.max(maxY, cell[1]);
            if (length != 2) {
                String zInfo = cellInfo[2];
                char[] chars = zInfo.toCharArray();
                cell[2] = chars[0];
            }
            cellFullInfo[i] = cell;
        }

        int[][] getIntArray = new int[maxX][maxY];
        for (int i = 0; i < count; i++) {
            int[] cell = cellFullInfo[i];
            int xIndex = cell[0] - 1;
            int yIndex = cell[1] - 1;
            if (cell[2] == 0) {
                getIntArray[xIndex][yIndex] = 0;
            }else{
                getIntArray[xIndex][yIndex] = cell[2];
            }
        }
        return getIntArray;
    }

    private int getLastInt(String info) {
        return Integer.valueOf(info.substring(info.length() - 1));
    }

}
